package com.atcd.token.demo.controller;


import com.atcd.token.demo.bean.User;
import com.atcd.token.demo.util.JwtUtil;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class loginController {


    @PostMapping("/login")
    @CrossOrigin //解决跨域问题
    @ResponseBody
    public String login(String userId, String username,String password){
        System.out.println(userId+" "+username+" "+password);
        if("admin".equals(username) && "123".equals(password)){ //相当于用户存在时访问
            //生成token
            String token = JwtUtil.createToken(new User(userId, username, password));
            //向浏览器返回一个json对象
               return token;
        }
        return null;
    }

    @GetMapping("/list")
    @CrossOrigin
    @ResponseBody
    public  String login (@RequestHeader("Authorization") String token){
        System.out.println(token);
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6IntcInBhc3N3b3JkXCI6XCIxMjNcIixcInVzZXJJZFwiOlwiMVwiLFwidXNlcm5hbWVcIjpcImFkbWluXCJ9IiwiZXhwIjoxNjUzNTc2NjcyfQ.r7kMGrTBcYIetG9dL9FXQZsGMxsAMiAPJh6npNqQdFE
        DecodedJWT token1 = JwtUtil.verifyToken(token);
        User user = JwtUtil.parseToken(token1);
        System.out.println(user);
        if("admin".equals(user.getUsername()) && "123".equals(user.getPassword())){
            System.out.println(user.getUsername()+" "+user.getPassword());
            return "ok";
        }

            return "no";
    }
}
